﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Apollo.Website
{
    public partial class EditFormEditor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string entityId = this.Request["EntityId"];
            Entity entity = Apollo.Application.Meta<Entity>()
                .SingleOrDefault<Entity>(en => en.Id.ToString() == entityId);

            if (entity != null) {
                ImageEntityIcon.ImageUrl = string.IsNullOrEmpty(entity.IconPath) ? "/css/icons/icon_01.png" : entity.IconPath;
                LabelEntityName.Text = entity.Name;

                TextBoxClientXml.Text = Apollo.Core.Form.GenerateDefault(entity).ToXml().ToString();

                return;
            }

            string entityEditId = this.Request["EntityEditId"];
            EntityEdit entityEdit = Apollo.Application.Meta<EntityEdit>()
                .SingleOrDefault<EntityEdit>(en => en.Id.ToString() == entityEditId);

            if (entityEdit != null)
            {
                ImageEntityIcon.ImageUrl = string.IsNullOrEmpty(entityEdit.Entity.IconPath) ? "/css/icons/icon_01.png" : entityEdit.Entity.IconPath;
                LabelEntityName.Text = entityEdit.Entity.Name + " [" + entityEdit.Name + "]";

                TextBoxClientXml.Text = entityEdit.EditString;

                return;
            }
            
        }
    }
}